package com.sevenGroup.pawnshop.controller;


import com.sevenGroup.pawnshop.Utils.Result;
import com.sevenGroup.pawnshop.controller.form.DelByIdsForm;
import com.sevenGroup.pawnshop.controller.form.FirstTypeForm;
import com.sevenGroup.pawnshop.controller.form.SelectDelForm;
import com.sevenGroup.pawnshop.controller.info.FirstTypeInfo;
import com.sevenGroup.pawnshop.controller.info.SelectDelInfo;
import com.sevenGroup.pawnshop.exception.sevenGroupException;
import com.sevenGroup.pawnshop.model.Select;
import com.sevenGroup.pawnshop.service.FirstTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@RestController
@RequestMapping("/firstType")
@Api(tags = "用于对商品大类的增删改查")
public class FirstTypeController {
    @Autowired
    FirstTypeService firstTypeService;
    /*新增或者更新必须有大类名称
    没有大类id则新增
    有大类id贼修改
    * */
    @ApiOperation("新增或者更新必须有大类名称" +
            " 没有大类id则新增" +
            " 有大类id贼修改")
    @PreAuthorize("hasAnyAuthority('boss','shopManage')")
    @PostMapping("saveAndUpdateFirstType")
    Result saveAndUpdateFirstType(@ApiParam("大类的参数对象") @RequestBody FirstTypeForm  firstTypeForm) throws sevenGroupException {
        firstTypeService.saveFirstType(firstTypeForm);
        return new Result<>().setState(200).setMessage("新增成功");
    }
    //查询所有大类分类
    @ApiOperation("查询所有大类分类")
    @GetMapping("selectAll")
    Result selectAll() throws sevenGroupException {
        List<FirstTypeInfo> firstTypeInfo = firstTypeService.findAll();
        return new Result().setState(200).setMessage("查询成功").setDate(firstTypeInfo);
    }
    //根据大类名称查询
    @ApiOperation("根据大类名称查询")
    @GetMapping("selectByName/{name}")
    Result selectByName(@ApiParam("根据名字模糊查询") @PathVariable String name) throws sevenGroupException {
        List<FirstTypeInfo> firstTypeInfo = firstTypeService.selectByName(name);
        return new Result().setState(200).setMessage("查询成功").setDate(firstTypeInfo);
    }
    //根据大类id删除
    @ApiOperation("根据大类id批量删除")
    @PreAuthorize("hasAnyAuthority('boss','shopManage')")
    @PostMapping("delById")
    Result delById(@ApiParam("id的参数对象，封装了一个集合")@RequestBody DelByIdsForm id) throws sevenGroupException {
        firstTypeService.del(id.getId());
        return new Result().setState(200).setMessage("删除成功");
    }
    @ApiOperation("鉴定图定义的转换工具")
    @PostMapping("selectDef")
    Result selectDef(@ApiParam("鉴定图定义数组") @RequestBody FirstTypeForm form){
        if (form.getSelect()==null) {
            return new Result();
        }else {
            List<SelectDelInfo> selectDelInfos = new ArrayList<>();
            for (Select select : form.getSelect()) {
                SelectDelInfo selectDelInfo = new SelectDelInfo();
                if (select.getRequired()!=null) {
                    selectDelInfo.setName(select.getRequired());
                    selectDelInfo.setState(0);
                    selectDelInfos.add(selectDelInfo);
                }else {
                    selectDelInfo.setName(select.getChoice());
                    selectDelInfo.setState(1);
                    selectDelInfos.add(selectDelInfo);
                }
            }
            return new Result().setDate(selectDelInfos);
        }
    }
   @ApiOperation("转换成鉴定图定义对象")
    @PostMapping("DefSelect")
    Result DefSelect(@ApiParam("定义图展示对象") @RequestBody SelectDelForm select ){
       if (select.getDefs()==null) {
           return new Result();
       }else {
           List<Select> selects = new ArrayList<>();
           for (SelectDelInfo def : select.getDefs()) {
               Select select1  = new Select();
               if (def.getState()==0) {
                   select1.setRequired(def.getName());
                    selects.add(select1);
               }else {
                   select1.setChoice(def.getName());
                   selects.add(select1);
               }
           }
           return new Result().setDate(selects);
       }
    }

}

